import React, { createContext, useReducer } from 'react';
import { ADD, SUB } from './constant';

export const MathContext = createContext();

function reducer(state, action) {
  switch (action.type) {
    case ADD:
      return state + (action.value || 1);
    case SUB:
      return state - (action.value || 1);
    default:
      return state;
  }
}
function reducer_1(state, action) {
  switch (action.type) {
    default:
      return (action && action.value) || state;
  }
}

export const MathStore = props => {
  const [state, dispatch] = useReducer(reducer, 0);
  const [value, dispatch_1] = useReducer(reducer_1, 5);
  return (
    <MathContext.Provider
      value={{ value: state, dispatch, constant: value, dispatch_1 }}
    >
      {props.children}
    </MathContext.Provider>
  );
};
